package model;

import Bean.Checkout;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.StringTokenizer;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

public class Export implements Serializable
{

    private Vector v;
    private DefaultTableModel tblModel;
    private Checkout checkout;

    public Export(Checkout checkout)
    {
        v = new Vector();
        this.checkout = checkout;

    }

    public int Count()
    {
        return v.size();
    }

    public void toHTML(String filename)
    {
        try
        {
            File f = new File(filename);
            if (!f.exists())
            {
                f.createNewFile();
            }
            Writer out = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(filename), "UTF8"));
            SimpleDateFormat dateformat = new SimpleDateFormat("dd-MM-yyyy");

            out.append("<html>");
            out.append("<head>");
            out.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
            out.append("<title>Hóa đơn</title>");
            out.append("</head>");

            out.append("<body>");
            out.append("<h1><strong><font color = \"#FF0066\"> Hóa đơn khách hàng</font></strong></h1>");
            out.append("<table width=\"327\" border=\"0\">");
            out.append("  <tr>");
            out.append("    <td width=\"119\">Tên khách hàng</td>");
            out.append("    <td width=\"198\">: " + checkout.getNamecustomer() + "</td>");
            out.append("  </tr>");
            out.append("  <tr>");
            out.append("    <td>Giới tính</td>");
            out.append("    <td>: " + checkout.getGenderCustomer() + "</td>");
            out.append("  </tr>");
            out.append("  <tr>");
            out.append("    <td>Năm sinh</td>");
            out.append("    <td>: " + checkout.getBirtday() + "</td>");
            out.append("  </tr>");
            out.append("  <tr>");
            out.append("    <td>CMND</td>");
            out.append("    <td>: " + checkout.getCMTND() + "</td>");
            out.append("  </tr>");
            out.append("  <tr>");
            out.append("    <td>Số điện thoại</td>");
            out.append("    <td>: " + checkout.getPhone() + "</td>");
            out.append("  </tr>");
            out.append("  <tr>");
            out.append("    <td>Phòng thuê</td>");
            out.append("    <td>: " + checkout.getNameroom() + "</td>");
            out.append("  </tr>");
            out.append("  <tr>");
            out.append("    <td>Loại phòng</td>");
            out.append("    <td>: " + checkout.getTyperoom() + "</td>");
            out.append("  </tr>");
            out.append("  <tr>");
            out.append("    <td>Ngày đến</td>");
            out.append("    <td>: " + dateformat.format(checkout.getDatecoming()) + "</td>");
            out.append("  </tr>");
            out.append("  <tr>");
            out.append("    <td>Ngày trả</td>");
            out.append("    <td>: " + dateformat.format(checkout.getDateleaving()) + "</td>");
            out.append("  </tr>");
            out.append("  <tr>");
            out.append("    <td>Đơn giá</td>");
            out.append("    <td>: " + checkout.getCostroom() + " VNĐ/ngày</td>");
            out.append("  </tr>");
            out.append("  <tr>");
            out.append("    <td>Tổng chi phí</td>");
            out.append("    <td>: " + checkout.getSumcost() + " VNĐ</td>");
            out.append("  </tr>");
            out.append("</table>");
            out.append("</body>");
            out.append("</html>");

            out.flush();
            out.close();
        }
        catch (FileNotFoundException fex)
        {
            JOptionPane.showInternalMessageDialog(null, fex.toString());
        }
        catch (IOException ioex)
        {
            JOptionPane.showInternalMessageDialog(null, ioex.toString());
        }
        catch (Exception ex)
        {
            JOptionPane.showInternalMessageDialog(null, "Error to export to " + filename + " with HTML format");
        }
    }
}
